<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<% String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>新增</title>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>
<script type="text/javascript">
    var id = '${id}';

    $(function () {
        if (id) {
            initData();
        } else {

        }
        $("#formSet1").ligerForm({
            validate: true
        });
        $("#formSet1").ligerForm();
        initPage();
        /* 清空 */
        $("#clearBtn").click(function (event) {
            $('#lnkCompId').val('');
            $('#lnkCompNm').val('');
            $('#dataId').val('');
            $('#dataNm').val('');
        });

        stepNumChange();
        if(id){
            disableFiled();
        }
    })

    function disableFiled() {
        if (liger.get("stepNum")) {
            liger.get("stepNum").setDisabled(true);
        }
        if(liger.get("stepRem")){
            $("#stepRem").setDisabled_ligerTextarea(true);
        }
        if (liger.get("starAmt_1")) {
            liger.get("starAmt_1").setDisabled(true);
        }
        if (liger.get("starAmt_2")) {
            liger.get("starAmt_2").setDisabled(true);
        }
        if (liger.get("starAmt_3")) {
            liger.get("starAmt_3").setDisabled(true);
        }
        if (liger.get("starAmt_4")) {
            liger.get("starAmt_4").setDisabled(true);
        }
        if (liger.get("endAmt_1")) {
            liger.get("endAmt_1").setDisabled(true);
        }
        if (liger.get("endAmt_2")) {
            liger.get("endAmt_2").setDisabled(true);
        }
        if (liger.get("endAmt_3")) {
            liger.get("endAmt_3").setDisabled(true);
        }
        if (liger.get("endAmt_4")) {
            liger.get("endAmt_4").setDisabled(true);
        }
        if (liger.get("rate_1")) {
            liger.get("rate_1").setDisabled(true);
        }
        if (liger.get("rate_2")) {
            liger.get("rate_2").setDisabled(true);
        }
        if (liger.get("rate_3")) {
            liger.get("rate_3").setDisabled(true);
        }
        if (liger.get("rate_4")) {
            liger.get("rate_4").setDisabled(true);
        }
        if (liger.get("prjStarTm")) {
            liger.get("prjStarTm").setDisabled(true);
        }
        if (liger.get("prjEndTm")) {
            liger.get("prjEndTm").setDisabled(true);
        }
        if (liger.get("isVipWork")) {
            liger.get("isVipWork").setDisabled(true);
        }

    }
    function  initPage() {
        $("#step_2").css('display','none');
        $("#step_3").css('display','none');
        $("#step_4").css('display','none');
        $("#starAmt_1").rules('add',{required:true});
        $("#endAmt_1").rules('add',{required:true});
        $("#rate_1").rules('add',{required:true});
    }
    function stepNumChange(){
        $("#stepNum").change(function(event) {
            bizTypCd = $("#stepNum").val();
            console.info("改变了")
            if(bizTypCd==1){
                console.info("改变了=====1")
                //账号类型对公
                $("#step_1").css('display','block');
                $("#step_2").css('display','none');
                $("#step_3").css('display','none');
                $("#step_4").css('display','none');
                $("#starAmt_1").rules('add',{required:true});
                $("#endAmt_1").rules('add',{required:true});
                $("#rate_1").rules('add',{required:true});
                $("#starAmt_2").rules('add',{required:false});
                $("#starAmt_3").rules('add',{required:false});
                $("#starAmt_4").rules('add',{required:false});
                $("#endAmt_2").rules('add',{required:false});
                $("#endAmt_3").rules('add',{required:false});
                $("#endAmt_4").rules('add',{required:false});
                $("#rate_4").rules('add',{required:false});
                $("#rate_3").rules('add',{required:false});
                $("#rate_2").rules('add',{required:false});
            }else if(bizTypCd==2){
                console.info("改变了=====2")
                $("#step_1").css('display','block');
                $("#step_2").css('display','block');
                $("#step_3").css('display','none');
                $("#step_4").css('display','none');
                $("#starAmt_1").rules('add',{required:true});
                $("#endAmt_1").rules('add',{required:true});
                $("#rate_1").rules('add',{required:true});
                $("#starAmt_2").rules('add',{required:true});
                $("#starAmt_3").rules('add',{required:false});
                $("#starAmt_4").rules('add',{required:false});
                $("#endAmt_2").rules('add',{required:true});
                $("#endAmt_3").rules('add',{required:false});
                $("#endAmt_4").rules('add',{required:false});
                $("#rate_4").rules('add',{required:false});
                $("#rate_3").rules('add',{required:false});
                $("#rate_2").rules('add',{required:true});
            }else if(bizTypCd==3){
                console.info("改变了=====3")
                $("#step_1").css('display','block');
                $("#step_2").css('display','block');
                $("#step_3").css('display','block');
                $("#step_4").css('display','none');
                $("#starAmt_1").rules('add',{required:true});
                $("#endAmt_1").rules('add',{required:true});
                $("#rate_1").rules('add',{required:true});
                $("#starAmt_2").rules('add',{required:true});
                $("#starAmt_3").rules('add',{required:true});
                $("#starAmt_4").rules('add',{required:false});
                $("#endAmt_2").rules('add',{required:true});
                $("#endAmt_3").rules('add',{required:true});
                $("#endAmt_4").rules('add',{required:false});
                $("#rate_4").rules('add',{required:false});
                $("#rate_3").rules('add',{required:true});
                $("#rate_2").rules('add',{required:true});
            }else if(bizTypCd==4){
                console.info("改变了=====4")
                $("#step_1").css('display','block');
                $("#step_2").css('display','block');
                $("#step_3").css('display','block');
                $("#step_4").css('display','block');
                $("#starAmt_1").rules('add',{required:true});
                $("#endAmt_1").rules('add',{required:true});
                $("#rate_1").rules('add',{required:true});
                $("#starAmt_2").rules('add',{required:true});
                $("#starAmt_3").rules('add',{required:true});
                $("#starAmt_4").rules('add',{required:true});
                $("#endAmt_2").rules('add',{required:true});
                $("#endAmt_3").rules('add',{required:true});
                $("#endAmt_4").rules('add',{required:true});
                $("#rate_4").rules('add',{required:true});
                $("#rate_3").rules('add',{required:true});
                $("#rate_2").rules('add',{required:true});
            }else{
                $("#step_1").css('display','none');
                $("#step_2").css('display','none');
                $("#step_3").css('display','none');
                $("#step_4").css('display','none');$("#starAmt_1").rules('add',{required:false});
                $("#endAmt_1").rules('add',{required:false});
                $("#rate_1").rules('add',{required:false});
                $("#starAmt_2").rules('add',{required:false});
                $("#starAmt_3").rules('add',{required:false});
                $("#starAmt_4").rules('add',{required:false});
                $("#endAmt_2").rules('add',{required:false});
                $("#endAmt_3").rules('add',{required:false});
                $("#endAmt_4").rules('add',{required:false});
                $("#rate_4").rules('add',{required:false});
                $("#rate_3").rules('add',{required:false});
                $("#rate_2").rules('add',{required:false});
            }
        });
    }

    function initData() {
        var _url = '<%=path%>/spl/rebates/getById.do';
        $.ajax({
            type: 'get',
            dataType: 'json',
            data: {id: id},
            url: _url,
            success: function (msg) {
                if (msg.success == 1) {
                    setData(msg.data);//设置数据
                } else {
                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                }
            }
        });
    }

    function setData(data) {
        $("#sId").val(data.id);
        $("#splId").val(data.splId);
        $("#splNm").val(data.splNm);
        $("#isVipWork").val(data.isVipWork);
        var form = liger.get("formSet1");
        form.setData({
            stepNum:data.stepNum,
            isVipWork:data.isVipWork
        });
        if(data.stepNum == 1){
            $("#step_1").css('display','block');
            $("#starAmt_1").rules('add',{required:true});
            $("#endAmt_1").rules('add',{required:true});
            $("#rate_1").rules('add',{required:true});
        }else if(data.stepNum == 2){
            $("#step_1").css('display','block');
            $("#step_2").css('display','block');
            $("#starAmt_1").rules('add',{required:true});
            $("#endAmt_1").rules('add',{required:true});
            $("#rate_1").rules('add',{required:true});
            $("#starAmt_2").rules('add',{required:true});
            $("#endAmt_2").rules('add',{required:true});
            $("#rate_2").rules('add',{required:true});
        }else if(data.stepNum == 3){
            $("#step_1").css('display','block');
            $("#step_2").css('display','block');
            $("#step_3").css('display','block');
            $("#starAmt_1").rules('add',{required:true});
            $("#endAmt_1").rules('add',{required:true});
            $("#rate_1").rules('add',{required:true});
            $("#starAmt_2").rules('add',{required:true});
            $("#starAmt_3").rules('add',{required:true});
            $("#endAmt_2").rules('add',{required:true});
            $("#endAmt_3").rules('add',{required:true});
            $("#rate_3").rules('add',{required:true});
            $("#rate_2").rules('add',{required:true});
        }else if(data.stepNum == 4){
            $("#step_1").css('display','block');
            $("#step_2").css('display','block');
            $("#step_3").css('display','block');
            $("#step_4").css('display','block');
            $("#starAmt_1").rules('add',{required:true});
            $("#endAmt_1").rules('add',{required:true});
            $("#rate_1").rules('add',{required:true});
            $("#starAmt_2").rules('add',{required:true});
            $("#starAmt_3").rules('add',{required:true});
            $("#starAmt_4").rules('add',{required:true});
            $("#endAmt_2").rules('add',{required:true});
            $("#endAmt_3").rules('add',{required:true});
            $("#endAmt_4").rules('add',{required:true});
            $("#rate_4").rules('add',{required:true});
            $("#rate_3").rules('add',{required:true});
            $("#rate_2").rules('add',{required:true});
        }

        $("#stepNum").val(data.stepNum);
        $("#starAmt_1").val(data.starAmt_1);
        $("#starAmt_2").val(data.starAmt_2);
        $("#starAmt_3").val(data.starAmt_3);
        $("#starAmt_4").val(data.starAmt_4);

        $("#endAmt_1").val(data.endAmt_1);
        $("#endAmt_2").val(data.endAmt_2);
        $("#endAmt_3").val(data.endAmt_3);
        $("#endAmt_4").val(data.endAmt_4);

        $("#rate_1").val(data.rate_1);
        $("#rate_2").val(data.rate_2);
        $("#rate_3").val(data.rate_3);
        $("#rate_4").val(data.rate_4);

        //日期处理
        if(data.prjStarTm){
            var date = new Date(data.prjStarTm);
            var dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
            $("#prjStarTm").val(dateStr);
        }
        if(data.prjEndTm){
            var date = new Date(data.prjEndTm);
            var dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
            $("#prjEndTm").val(dateStr);
        }

        if(data.indateStar){
            var date = new Date(data.indateStar);
            var dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
            $("#indateStar").val(dateStr);
        }
        if(data.indateEnd){
            var date = new Date(data.indateEnd);
            var dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
            $("#indateEnd").val(dateStr);
        }

        $("#crtUsrNm").val(data.crtUsrNm);
        $("#stepRem").val(data.stepRem);
        /*var form = liger.get("formSet1");
        form.setData({
            starLevel: data.starLevel,
            dataSource: data.dataSource
        });*/

    }
    //定义父级调用事件
    frameElement.dialog.onOk = function () {
        flag = true;
        save();
    }

    frameElement.dialog.onCancel = function () {
        frameElement.dialog.close();
    }

    function checkOnly(){
        var _id = $("#sId").val();
            var _splId = $("#splId").val();
            var _splNm = $("#splNm").val();
            var _isVipWork = $("#isVipWork").val();
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: {splId:_splId,splNm:_splNm,id:_id,isVipWork:_isVipWork},
                url: '<%=path%>/spl/rebates/checkOnly.do',
                beforeSend: function () {
                    wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                },
                success: function (msg) {
                    wait_dialog.close();
                    if (msg.success == 1) {
                       save();
                    } else {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                    }
                }
            });
    }
    function save() {
        var form = liger.get("formSet1");
        if (!form.valid()) {
            $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
            return false; //验证不通过，不执行以下代码
        }
        var data = form.getData();
        var _splId = $("#splId").val();
        var _splNm = $("#splNm").val();
        var _isVipWork = $("#isVipWork").val();
        if(!_isVipWork){
            _isVipWork = "0";
        }
        data["splId"] = _splId;
        data["splNm"] = _splNm ;
        data["isVipWork"] = _isVipWork ;
        var _id = $("#sId").val();
        var url = '';
        var flag = false ;
        if (_id) {
            url = '<%=path%>/spl/rebates/modify.do';
            data["id"] = _id;
            flag = true ;
        } else {
            url = '<%=path%>/spl/rebates/add.do';
        }
        var wait_dialog;
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: data,
            url: url,
            beforeSend: function () {
                wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
            },
            success: function (msg) {
                wait_dialog.close();
                if (msg.success == 1) {
                    $("#sId").val(msg.data);
                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                    if(flag) {
                        parent.callback();
                    }
                    frameElement.dialog.close();
                } else {
                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                }
            }
        });
    }
</script>
<body class="iframeBody">
<div class="Wrapper">
    <div id="pageInTab" class="pageInTab">
        <div tabid="tabA" title="基本信息" lselected="true">
            <form id="formSet1">
                <div class="fieldset1 tabel_col clearFix">
                    <c:if test="${id != null || isVip==1 }">
                        <div class="fieldBox col_50">
                            <div class="label">是否大客户业务政策</div>
                            <div class="field">
                                <select name="isVipWork" id="isVipWork" data-value=" "
                                        validate="{required:true,select:true}">
                                    <option value="0" selected="selected">否</option>
                                    <option value="1" >是</option>
                                </select>
                                <span class="requiredSymbol">*</span>
                            </div>
                        </div>
                    </c:if>
                    <div class="fieldBox col_50">
                        <div class="label">阶梯数量</div>
                        <div class="field">
                            <select name="stepNum" id="stepNum" data-value=" "
                                    validate="{required:true,select:true}">
                                <option value="1" selected="selected">1</option>
                                <option value="2" >2</option>
                                <option value="3" >3</option>
                                <option value="4" >4</option>
                            </select>
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_100_auto">
                        <div class="label">阶梯区间</div>
                        <div class="field" id = "step_1">
                            <input type="text" id="starAmt_1" name="starAmt_1" class="ui-textbox" />W
                            -
                            <input type="text" id="endAmt_1" name="endAmt_1" class="ui-textbox" />W，
                            <input type="text" id="rate_1" name="rate_1" class="ui-textbox" />%；
                            <span class="requiredSymbol">*</span>
                        </div>
                        <div class="field" id="step_2">
                            <input type="text" id="starAmt_2" name="starAmt_2" class="ui-textbox" />
                            W-
                            <input type="text" id="endAmt_2" name="endAmt_2" class="ui-textbox" />
                            W，
                            <input type="text" id="rate_2" name="rate_2" class="ui-textbox" />%；
                            <span class="requiredSymbol">*</span>
                        </div>
                        <div class="field" id="step_3">
                            <input type="text" id="starAmt_3" name="starAmt_3" class="ui-textbox" />
                            W-
                            <input type="text" id="endAmt_3" name="endAmt_3" class="ui-textbox" />
                            W，
                            <input type="text" id="rate_3" name="rate_3" class="ui-textbox" />%；
                            <span class="requiredSymbol">*</span>
                        </div>
                        <div class="field" id="step_4">
                            <input type="text" id="starAmt_4" name="starAmt_4" class="ui-textbox" />
                            W-
                            <input type="text" id="endAmt_4" name="endAmt_4" class="ui-textbox" />
                            W，
                            <input type="text" id="rate_4" name="rate_4" class="ui-textbox" />%；
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_100_auto">
                        <div class="label">可申请阶梯返利项目范围</div>
                        <div class="field">
                            <input width="20px" name="prjStarTm" id="prjStarTm" type="text" class="ui-datepicker" value=""  validate="{required:true}">
                            -
                            <input width="20px" name="prjEndTm" id="prjEndTm" type="text" class="ui-datepicker" value=""  validate="{required:true}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_100_auto">
                        <div class="label">有效期</div>
                        <div class="field">
                            <input name="indateStar" id="indateStar" type="text" class="ui-datepicker" value="" validate="{required:true}">
                            -
                            <input name="indateEnd" id="indateEnd" type="text" class="ui-datepicker" value="" validate="{required:true}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_50">
                        <div class="label">添加人</div>
                        <div class="field">
                            <input name="crtUsrNm" id="crtUsrNm" type="text" value="${crtUsrNm}"
                                   class="ui-textbox" disabled="disabled" readonly="readonly">
                            <input name="crtUsrId" id = "crtUsrId" value="${crtUsrId}" hidden="hidden">
                        </div>
                    </div>
                    <div class="fieldBox col_100">
                        <div class="label">备注</div>
                        <div class="field">
                        <textarea name="stepRem" id="stepRem" rows="4"
                                  class="l-textarea" style="width: 99%" value=""></textarea>
                        </div>
                    </div>
                    <input name="sId" id="sId" type="hidden" value="${id }">
                    <input name="splId" id="splId" type="hidden" value="${splId }">
                    <input name="splNm" id="splNm" type="hidden" value="${splNm }">
                </div>
                <div class="btnsBox">
                    <span id="saveBtn1" onclick="checkOnly();" class="sq-button sq-button-round">保存</span>
                </div>
            </form>
        </div>
    </div>
</div>

</body>
</html>